package com.jackie.setting

import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    private val sharedPreferences: SharedPreferences by lazy {
        getSharedPreferences(packageName, MODE_PRIVATE)
    }

    private val settings: List<SettingsItem> by lazy {
        val name = sharedPreferences.getString(ARG_NAME, "A")
        listOf(
            SettingsItem(R.mipmap.icon_personal, R.string.name, name),
            SettingsItem(R.mipmap.icon_store, R.string.store),
            SettingsItem(R.mipmap.icon_bind, R.string.bind, type = SettingsAdapter.SWITCH_TYPE),
            SettingsItem(
                R.mipmap.icon_version,
                R.string.version,
                "1.0",
                SettingsAdapter.SPECIAL_TYPE
            ),
            SettingsItem(
                R.mipmap.icon_logout,
                R.string.logout,
                type = SettingsAdapter.SPECIAL_TYPE
            ),
        )
    }

    private val mRec: RecyclerView by lazy {
        findViewById(R.id.recycler_view)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mRec.adapter = SettingsAdapter(this, settings).also {
            it.onItemClickListener = { _, _, position ->
                when (position) {
                    0 -> {
                        NameSelectorActivity.showForResult(this, settings[0].des, REQUEST_CODE)
                    }
                    4 -> {
                        val onClickListener = DialogInterface.OnClickListener { dialog, _ ->
                            dialog?.dismiss()
                        }

                        AlertDialog.Builder(this)
                            .setTitle(getString(R.string.dialog_alert))
                            .setMessage(getString(R.string.dialog_ask_logout))
                            .setPositiveButton(R.string.logout, onClickListener)
                            .setNegativeButton(R.string.cancel, onClickListener)
                            .show()
                    }
                }
            }
        }

        val dividerItemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        dividerItemDecoration.setDrawable(
            ColorDrawable(
                ContextCompat.getColor(
                    this,
                    R.color.backgroundColor
                )
            )
        )
        mRec.addItemDecoration(dividerItemDecoration)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            Activity.RESULT_OK -> {
                data?.getStringExtra(ARG_NAME)?.let {
                    settings[0].des = it
                    mRec.adapter?.notifyItemChanged(0)
                }
            }
            else -> {
            }
        }
    }

    override fun onStop() {
        super.onStop()
        sharedPreferences
            .edit()
            .putString(ARG_NAME, settings[0].des)
            .apply()
    }

    companion object {
        const val ARG_NAME = "name"
        private const val REQUEST_CODE = 100
    }
}